import * as path from 'path'
import i18n from '@/i18n'
import { Assist } from '@libs/lhy-common'
import { CoreConfig, CoreConfigProperty } from '@libs/lhy-core'

export class AppConfig extends CoreConfig {
  // 上传配置
  @CoreConfigProperty()
  upload: {
    // 单文件大小限制，单位: MB
    singleFileSize: number
    // meta存储相对目录
    metaImageStorePath: string
    // flow存储相对目录
    flowImageStorePath: string
  }

  /**
   * 获取meta图片文件地址
   * @param fileName
   * @returns
   */
  getMetaUploadImageStorePath(fileName: string) {
    return this.getUploadImageStorePath(this.upload.metaImageStorePath, fileName)
  }

  /**
   * 获取meta图片文件地址
   * @param fileName
   * @returns
   */
  getFlowUploadImageStorePath(fileName: string) {
    return this.getUploadImageStorePath(this.upload.flowImageStorePath, fileName)
  }

  /**
   * 获取图片文件地址
   * @param imageStorePath
   * @param fileName
   * @returns
   */
  protected getUploadImageStorePath(imageStorePath: string, fileName: string) {
    const publicStatic = this.getStaticItem('public')
    Assist.threw(!publicStatic, i18n.t('server.entity.app-config.1215075-0'))

    const storePath = path.join(publicStatic.storeRoot, imageStorePath, fileName)
    const storeUrl = `${publicStatic.urlPrefix}/${imageStorePath}/${fileName}`

    return {
      storePath,
      storeUrl,
    }
  }
}
